home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 21 / emacsrc / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-14  |  3.4 KB  |  170 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here. A better message writing scheme should
  4.  * be used.
  5.  */
  6.  
  7. #include        "ed.h"
  8. #include    "osbind.h"
  9.  
  10. #define        SZtextbuf 512
  11. static int    fd;                     /* File descriptor, all functions. */
  12. static int    rwflag;            /* 0=read or 1=write flag */
  13. static char    textbuf[SZtextbuf];
  14. static int    x = 0;
  15. static int    n = 0;
  16. static int    sz = SZtextbuf;
  17. static char    *txtp;
  18. static int    lnn;            /* line number            */
  19.  
  20. static                    /* allocate large buffer for    */
  21. fbfalloc()                /* file io            */
  22. {
  23.     txtp = (char *) malloc(sz = 16*1024);
  24.     if (txtp == NULL) {
  25.         txtp = textbuf;
  26.         sz = SZtextbuf;
  27.     }
  28.     rwflag = x = n = 0;
  29. }
  30.  
  31. static
  32. fbffree()
  33. {
  34.     if (txtp != textbuf) free(txtp);
  35.     rwflag = x = n = 0;    /* not nec    */
  36.     sz = SZtextbuf;        /* not nec    */
  37. }
  38.  
  39. /*
  40.  * Open a file for reading.
  41.  */
  42. ffropen(fn)
  43. char    *fn;
  44. {
  45.         if ((fd=Fopen(fn, 0)) < 0) return (FIOFNF);
  46.     fbfalloc();
  47.     lnn = 0;
  48.         return (FIOSUC);
  49. }
  50.  
  51. /*
  52.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  53.  * (cannot create).
  54.  */
  55. ffwopen(fn)
  56. char    *fn;
  57. {
  58.     char    lfn[NFILEN];
  59.  
  60.     strcpy(lfn, fn);    /* ST BIOS uppercases filename! */
  61.         if ((fd=Fcreate(lfn, 0)) < 0) {
  62.                 mlwrite("Cannot open file for writing");
  63.                 return (FIOERR);
  64.         }
  65.     fbfalloc();
  66.     rwflag = 1;
  67.         return (FIOSUC);
  68. }
  69.  
  70. /*
  71.  * Close a file. Should look at the status in all systems.
  72.  */
  73. ffclose()
  74. {
  75.     if ((rwflag == 1) && (x < sz)) {
  76.         if (Fwrite(fd, (long) x, txtp) < 0) {
  77.                     mlwrite("Write I/O error");
  78.                     return (FIOERR);
  79.         }
  80.     }
  81.         Fclose(fd);    /* check for errors ??    */
  82.     fbffree();
  83.         return (FIOSUC);
  84. }
  85.  
  86. /*
  87.  * Write a line to the already opened file. The "buf" points to the buffer,
  88.  * and the "nbuf" is its length, less the free newline. Return the status.
  89.  * Check only at the newline.
  90.  */
  91. ffputline(buf, nbuf)
  92. char    buf[];
  93. {
  94.         register    int    i;
  95.     register    char    *p;
  96.     static        char    crlf[] = "\r\n";
  97.  
  98.     p = buf;
  99.     xxxx:
  100.     for (i = 0; i < nbuf; ++i) {
  101.         txtp[x++] = *p++;
  102.         if (x == sz) {
  103.             if (Fwrite(fd, (long) x, txtp) < 0) {
  104.                         mlwrite("Write I/O error");
  105.                         return (FIOERR);
  106.             }
  107.             x = 0;
  108.         }
  109.     }
  110.     if (p != &crlf[2]) {
  111.         p = crlf;
  112.         nbuf = 2;
  113.         goto xxxx;
  114.     }
  115.         return (FIOSUC);
  116. }
  117.  
  118. /*
  119.  * Read a line from a file, and store the bytes in the supplied buffer. The
  120.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  121.  * at the end of the file that don't have a newline present. Check for I/O
  122.  * errors too. Return status.
  123.  */
  124. ffgetline(buf, nbuf, nb)
  125. register char   buf[];
  126. int        nbuf;
  127. int        *nb;
  128. {
  129.         register int    i;
  130.     register char   c;
  131.  
  132.         i = 0;
  133.     for (;;) {
  134.         if (x == n) {
  135.             n = Fread(fd, (long) sz, txtp);
  136.             x = 0;
  137.         }
  138.         if (x >= n) break;
  139.         c = txtp[x++];
  140.         if (c == '\n') {
  141.             lnn ++;
  142.             if (i > 0 && buf[i-1] == '\r') i--;
  143.             break;
  144.         }
  145.         buf[i++] = c;
  146.         if (i == nbuf) {
  147.             mlwrite("long line %d is being split...", lnn);
  148.                        break;
  149.         }
  150.         }
  151.     buf[i] = '\000';
  152.     *nb = i;
  153.  
  154.     if (x > n) {
  155.         mlwrite("File read error");
  156.         return (FIOERR);
  157.     }
  158.  
  159.     if (n == 0) {
  160.         if (i != 0) {
  161.             mlwrite("The last line ended without \\n");
  162.             return (FIOERR);
  163.         }
  164.         return (FIOEOF);
  165.     }
  166.         return (FIOSUC);
  167. }
  168.  
  169. /* -eof- */
  170.